/*
 * Copyright 2014 OpenMarket Ltd
 * Copyright 2017 Vector Creations Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.matrix.androidsdk.rest.api;

import org.matrix.androidsdk.RestClient;
import org.matrix.androidsdk.rest.model.BulkLookupParams;
import org.matrix.androidsdk.rest.model.BulkLookupResponse;
import org.matrix.androidsdk.rest.model.IdentityServerRequest3PIDValidationParams;
import org.matrix.androidsdk.rest.model.IdentityServerRequestTokenResponse;
import org.matrix.androidsdk.rest.model.RequestEmailValidationParams;
import org.matrix.androidsdk.rest.model.RequestOwnershipParams;
import org.matrix.androidsdk.rest.model.SuccessResult;
import org.matrix.androidsdk.rest.model.identityserver.HashDetailResponse;
import org.matrix.androidsdk.rest.model.identityserver.LookUpV2Params;
import org.matrix.androidsdk.rest.model.identityserver.LookUpV2Response;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface IdentityThirdPidApi {

    /**
     * Request the hash detail to request a bunch of 3PIDs
     */
    @GET(RestClient.URI_IDENTITY_PATH_V2 + "hash_details")
    Call<HashDetailResponse> hashDetails();

    /**
     * Request a bunch of 3PIDs
     *
     * @param body the body request
     */
    @POST(RestClient.URI_IDENTITY_PATH_V2 + "lookup")
    Call<LookUpV2Response> bulkLookupV2(@Body LookUpV2Params body);

    /**
     * Request the ownership validation of an email address or a phone number previously set
     * by {@link ProfileApi#requestEmailValidation(RequestEmailValidationParams)}
     *
     * @param medium the medium of the 3pid
     */
    @POST(RestClient.URI_IDENTITY_PATH_V2 + "validate/{medium}/submitToken")
    Call<SuccessResult> requestOwnershipValidationV2(@Path("medium") String medium,
                                                     @Body RequestOwnershipParams body);

    /* ==========================================================================================
     * Legacy requests
     * ========================================================================================== */

    /**
     * Request a bunch of 3PIDs
     * <p>
     * Ref: https://matrix.org/docs/spec/identity_service/unstable#post-matrix-identity-api-v1-bulk-lookup
     *
     * @param body the body request
     */
    @POST(RestClient.URI_IDENTITY_PATH + "bulk_lookup")
    Call<BulkLookupResponse> bulkLookup(@Body BulkLookupParams body);


    /**
     * Request the ownership validation of an email address or a phone number previously set
     * by {@link ProfileApi#requestEmailValidation(RequestEmailValidationParams)}
     * <p>
     * Ref: https://matrix.org/docs/spec/identity_service/unstable#post-matrix-identity-api-v1-validate-email-submittoken for email
     * Ref: https://matrix.org/docs/spec/identity_service/unstable#post-matrix-identity-api-v1-validate-msisdn-submittoken for phone
     *
     * @param medium       the medium of the 3pid
     * @param token        the token generated by the requestToken call
     * @param clientSecret the client secret which was supplied in the requestToken call
     * @param sid          the sid for the session
     */
    @POST(RestClient.URI_IDENTITY_PATH + "validate/{medium}/submitToken")
    Call<SuccessResult> requestOwnershipValidation(@Path("medium") String medium,
                                                   @Query("token") String token,
                                                   @Query("client_secret") String clientSecret,
                                                   @Query("sid") String sid);


    /**
     * Create a session to change the bind status of an email to an identity server
     *
     * @param params
     * @return the sid
     */
    @POST(RestClient.URI_IDENTITY_PATH_V2 + "validate/email/requestToken")
    Call<IdentityServerRequestTokenResponse> requestMailValidationToken(@Body IdentityServerRequest3PIDValidationParams params);

    /**
     * Create a session to change the bind status of an phone number to an identity server
     *
     * @param params
     * @return the sid
     */
    @POST(RestClient.URI_IDENTITY_PATH_V2 + "validate/msisdn/requestToken")
    Call<IdentityServerRequestTokenResponse> requestPhoneNumberValidationToken(@Body IdentityServerRequest3PIDValidationParams params);

}
